package palmerpuric.tweets2bits;

import flash.text.TextField;
import twitter.api.Twitter;
import twitter.api.TwitterSearch;
import twitter.api.data.TwitterStatus;
import twitter.api.events.TwitterEvent;

/**
 * Razred za iskanje
 */
class TweetsFinder {

	private var _twitter:Twitter;
	private var _twitterSearch:TwitterSearch;
	private var _picBitsMaker:PicBitsMaker;
	private var _keyword:String;
	
	public function new(resultBox:TextField) : Void {
		_twitter = new Twitter();
		_twitterSearch = new TwitterSearch();
		_picBitsMaker = new PicBitsMaker(resultBox);
	}
	
	public function search(keyword:String) : Void {
		// keyword za iskanje
		_twitterSearch.addKeyword(keyword);
		// za kasnejse oznacevanje keywordov v tweetu
		_keyword = keyword;
		// iskanje
		_twitter.search(_twitterSearch);
		_twitter.addEventListener(TwitterEvent.ON_SEARCH, searchCompleteListener);
	}
	
	private function searchCompleteListener(e:TwitterEvent) {
		e.target.removeEventListener(TwitterEvent.ON_SEARCH, searchCompleteListener);
		//_picBitsMaker = new PicBitsMaker();
		// samo prvi najden tweet
		var tweet:TwitterStatus = Reflect.field(e.data, "0");
		_picBitsMaker.doMagic(tweet.text.toLowerCase(), _keyword);
	}
}
